/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.phonegroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.phonegroup.Phonegroup;
import com.unis.service.phonegroup.PhonegroupService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-01-28
 */
@Controller
@RequestMapping("/phonegroup")
public class PhonegroupController {

    private PhonegroupService phonegroupService;

    /****************************************
     * Insert a instace of Phonegroup
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertPhonegroup(@RequestBody Phonegroup phonegroup) throws Exception {
        phonegroup.setStatmentId("Phonegroup.insert");
        String status = "failure";
        phonegroupService.insert(phonegroup);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Phonegroup
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Phonegroup phonegroup) throws Exception {
        phonegroup.setStatmentId("Phonegroup.update");
        String status = "failure";
        phonegroupService.update(phonegroup);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Phonegroup
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Phonegroup phonegroup) throws Exception {
        phonegroup.setStatmentId("Phonegroup.delete");
        String status = "failure";
        phonegroupService.batchDelete(phonegroup);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Phonegroup
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Phonegroup phonegroup = new Phonegroup();
        phonegroup.setStatmentId("Phonegroup.queryAsList");
        BeanUtils.populate(phonegroup, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(phonegroup, pageMap);
        List<BaseModel> phonegroupList = phonegroupService.queryAsList(phonegroup);
        Map<String, Object> renderMap = GridUIMapper.render(phonegroup, phonegroupList);
        return renderMap;
    }

    /****************************************
     * 查询分类树形结构(一级)
     ****************************************/
    @RequestMapping("/queryGroupLevel")
    @ResponseBody
    public Object queryGroupAsTreeWithOneLayer() throws Exception {
        Phonegroup phonegroup = new Phonegroup();
        phonegroup.setStatmentId("Phonegroup.queryAsList");
        phonegroup.setPaging(false);
        List<BaseModel> phonegroupList = phonegroupService.queryAsList(phonegroup);
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("groupname", "通讯录分组");
        root.put("children", phonegroupList);
        return root;
    }

    /****************************************
     * 选单
     ****************************************/
    @RequestMapping("/select")
    @ResponseBody
    public List<?> getSelectData() throws Exception {
        List<Object> list = new ArrayList<Object>();

        Phonegroup phonegroup = new Phonegroup();
        phonegroup.setStatmentId("Phonegroup.queryAsList");
        phonegroup.setPaging(false);
        List<BaseModel> phonegroupList = phonegroupService.queryAsList(phonegroup);

        for (BaseModel baseModel : phonegroupList) {
            Map<String, Object> map = new HashMap<String, Object>();
            Phonegroup pg = (Phonegroup) baseModel;
            map.put("value", pg.getGroupid());
            map.put("label", pg.getGroupname());
            list.add(map);
        }
        return list;
    }

    /****************************************
     * Query a instaces of Phonegroup
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Phonegroup phonegroup) throws Exception {
        phonegroup.setStatmentId("Phonegroup.queryAsObject");
        return phonegroupService.queryAsObject(phonegroup);
    }

    @RequestMapping("/page")
    public String forword() {
        return "phonegroup/phonegroupList";
    }

    @RequestMapping("/pagenotshared")
    public String forwordit() {
        return "phonegroup/phonegroupListNotShared";
    }
    
    @Resource
    public void setPhonegroupService(PhonegroupService phonegroupService) {
        this.phonegroupService = phonegroupService;
    }
}
